﻿using System;
using UnityEngine;
using Yoozoo.HRP.Runtime;
using Yoozoo.UI.YGUI;

namespace Yoozoo.Gameplay.Liberty.PlayerController
{
    public class PlayerStaminaBar : MonoBehaviour
    {
        public Transform followTarget;
        public Vector3 followOffset;
        
        public Color green = Color.green;
        public Color yellow = Color.yellow;
        public Color red = Color.red;
        
        public YImage bgBlack;
        public YImage bgRed;
        public YImage bar;

        private Camera _UICamera;
        public Camera UICamera
        {
            get
            {
                if (_UICamera == null)
                {
                    var go = GameObject.Find("UIRoot/UICamera");
                    _UICamera = go.GetComponent<Camera>();
                }

                return _UICamera;
            }
        }

        private Vector2 _lastAnchoredPosition;
        public float lerpSpeed = 10;

        private CanvasGroup _canvasGroup;
        private float _currentAlpha = 0;
        private float _targetAlpha = 0;
        [SerializeField]
        private float _alphaLerpSpeed = 1;
        
        private void Awake()
        {
            _canvasGroup = GetComponent<CanvasGroup>();
            _canvasGroup.alpha = 0f;
            _currentAlpha = 0;
            _targetAlpha = 0;
        }

        private void FixedUpdate()
        {
            UpdatePos();
            UpdateAlpha();
        }

        private void UpdatePos()
        {
            if (followTarget)
            {
                var hCamera = HCameraManager.GetBaseCamera();
                if (hCamera)
                {
                    Vector3 dir = (followTarget.position - hCamera.transform.position).normalized;
                    dir.y = 0;
                    dir = Vector3.Cross(dir.normalized, Vector3.up);

                    Vector3 sPos = hCamera.Camera.WorldToScreenPoint(followTarget.position + followOffset.x * dir + Vector3.up * followOffset.y);
                    RectTransformUtility.ScreenPointToLocalPointInRectangle((RectTransform)transform.parent, 
                        new Vector2(sPos.x, sPos.y), UICamera, out var rectPos);

                    Vector2 r = rectPos;
                    float d = Vector2.Distance(rectPos, _lastAnchoredPosition);
                    if (d < 100)
                    {
                        r = Vector2.Lerp(_lastAnchoredPosition, rectPos, lerpSpeed * Time.fixedDeltaTime);
                    }
                    else
                    {
                        r = Vector2.Lerp(_lastAnchoredPosition, rectPos, lerpSpeed * Time.fixedDeltaTime * d / 50);
                    }
                    _lastAnchoredPosition = r;

                    ((RectTransform)transform).anchoredPosition = r;
                }
            }
        }

        private void UpdateAlpha()
        {
            _currentAlpha = Mathf.Lerp(_currentAlpha, _targetAlpha, Time.fixedDeltaTime * _alphaLerpSpeed);
            if (Math.Abs(_canvasGroup.alpha - _currentAlpha) > 0.002f)
            {
                _canvasGroup.alpha = _currentAlpha;
            }
        }
        
        public void SetFollowTransform(Transform follow)
        {
            followTarget = follow;
        }
        
        public void SetBarValue(float v)
        {
            v = Mathf.Max(0, v);
            bar.fillAmount = v;

            if (v < 1)
            {
                UpdatePos();
                _targetAlpha = 1f;
            }
            else
            {
                _targetAlpha = 0f;
            }
        }
        
        public void SwitchState(int t)
        {
            if (t == 1) // 绿色
            {
                //bgBlack.gameObject.SetActive(true);
                bgRed.gameObject.SetActive(false);
                bar.color = green;
            }
            else if (t == 2) // 黄色
            {
                //bgBlack.gameObject.SetActive(true);
                bgRed.gameObject.SetActive(false);
                bar.color = yellow;
            }
            else if (t == 3) // 红色
            {
                //bgBlack.gameObject.SetActive(false);
                bgRed.gameObject.SetActive(true);
                bar.color = red;
            }
        }

    }
}